package com.olymtech.jbricks.jbricks.config;

import com.codahale.metrics.*;
import metrics_influxdb.InfluxdbReporter;
import metrics_influxdb.api.protocols.InfluxdbProtocols;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class MetricsConfig {

    @Bean
    public MetricRegistry registry() {
        MetricRegistry registry = new MetricRegistry();

        // 输出到控制台
//        ConsoleReporter reporter = ConsoleReporter.forRegistry(registry)
//                .convertRatesTo(TimeUnit.SECONDS)
//                .convertRatesTo(TimeUnit.SECONDS)
//                .build();
//        reporter.start(5,TimeUnit.SECONDS);

        ScheduledReporter reporter = InfluxdbReporter.forRegistry(registry)
                // influxdb的ip,port，用户名,密码,数据库
                .protocol(InfluxdbProtocols.http("localhost",8086,"root","root","db0"))
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .filter(MetricFilter.ALL)
                .skipIdleMetrics(false)
                .build();
        reporter.start(5,TimeUnit.SECONDS);

        return registry;
    }

    @Bean
    public Timer getUserTime(MetricRegistry registry) {
        Timer timer = registry.timer("getUserTimeTimer");
        return timer;
    }

    @Bean
    public Meter userControllerMeter(MetricRegistry registry) {
        Meter meter = registry.meter("userControllerMeter");
        return meter;
    }

    @Bean
    public Histogram histogram(MetricRegistry registry) {
        Histogram histogram = registry.histogram("histogram");
        return histogram;
    }


}